//
//  ViewController.m
//  ExcelDemo
//
//  Created by chenlishuang on 16/6/12.
//  Copyright © 2016年 chenlishuang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createXml];
}
- (void)createXml{
    NSMutableString *xlsStr= [[NSMutableString alloc]init];
    [xlsStr appendFormat:@"%@\t%@\t%@\t%@\t%c",@"one",@"two",@"three",@"four",'\n'];
    [xlsStr appendFormat:@"%@\t%@\t%@\t%@\t%c",@"one1",@"two2",@"three3",@"four4",'\n'];

    //将NSString类型转成NSUTF8String编码类型
    if (xlsStr.length) {
        NSData *xlsData = [xlsStr dataUsingEncoding:NSUTF16StringEncoding];
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
        NSString *xlsFilePath = [docPath stringByAppendingPathComponent:@"myXls.xls"];
        NSLog(@"===%@",xlsFilePath);
        if (![[NSFileManager defaultManager] fileExistsAtPath:xlsFilePath]) {
            [[NSFileManager defaultManager] createFileAtPath:xlsFilePath contents:nil attributes:nil];
        }else{
            [[NSFileManager defaultManager] removeItemAtPath:xlsFilePath error:nil];
            [[NSFileManager defaultManager] createFileAtPath:xlsFilePath contents:nil attributes:nil];
        }
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:xlsFilePath];
        [fileHandle writeData:xlsData];
        [fileHandle closeFile];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
